﻿using Microsoft.SharePoint;

namespace SPPropertyBagViewer.Helpers
{
    public static class CustomActionHelper
    {
        public static void DeleteEcbCustomAction(this SPList spList, string title)
        {
            var name = title.Replace(" ", "");
            spList.RunAsElevated((list) =>
            {
                SPUserCustomActionCollection customActions = list.UserCustomActions;
                foreach (var customAction in customActions)
                {
                    if (customAction.Name == name && customAction.Location == "EditControlBlock")
                    {
                        customAction.Delete();
                        list.Update();
                        break;
                    }
                }
            });
        }

        public static void AddEcbCutomAction(this SPList spList, string title, string url, SPBasePermissions right = SPBasePermissions.EditListItems)
        {
            var name = title.Replace(" ", "");
            spList.RunAsElevated((list) =>
            {
                SPUserCustomAction customaction = list.UserCustomActions.Add();
                customaction.Name = name;
                customaction.Rights = right;
                customaction.Location = "EditControlBlock";
//                string cAction = @"javascript: var options = {
//                                                url: '{SiteUrl}' + '/_layouts/SPPropertyBagViewer/SPPropertyBagViewer.aspx?ListId={ListId}&amp;ItemId={ItemId}',
//                                                allowMaximize: false,
//                                                width: 500,
//                                                height: 440 };
//                                            SP.UI.ModalDialog.showModalDialog(options);";
                customaction.Url =url; //cAction;
                customaction.Sequence = 10001;
                customaction.Title = title;
                customaction.Update();
                list.Update();
            });
        }

    }
}
